import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import DictVectorizer
from sklearn.tree import DecisionTreeClassifier, export_graphviz


def decision_tree_titanic():
    """
    决策树进行乘客生存预测
    :return:
    """
    # 1、获取数据
    titan = pd.read_csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt")
    # 2、数据的处理
    x = titan[['pclass', 'age', 'sex']]
    y = titan['survived']
    # print(x , y)
    # 2.1 缺失值需要处理，将特征当中有类别的这些特征进行字典特征抽取
    x['age'].fillna(x['age'].mean(), inplace=True)
    # 2.2 对于x转换成字典数据
    x = x.to_dict(orient="records")
    # 3.分割训练集合测试集
    x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22)
    # 4.字典特征抽取
    transfer = DictVectorizer()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)

    # 4.决策树预估器 max_depth:决策树深度
    estimator = DecisionTreeClassifier(criterion="entropy", max_depth=8)
    estimator.fit(x_train, y_train)
    # 4.模型评估
    # 方法一：直接比对预测值与真实值
    y_predict = estimator.predict(x_test)
    print("y_predict:\n", y_predict)
    print("y_test:\n", y_test)
    print("预测值与真实值比对：\n", y_predict == y_test)
    # 方法二：计算准确率
    score = estimator.score(x_test, y_test)
    print("准确率为：\n", score)

    # 5.可视化决策树
    export_graphviz(estimator, out_file="titanic_tree.dot", feature_names=transfer.get_feature_names_out())

    return None

if __name__ == "__main__":
    decision_tree_titanic()